﻿using System;
using Noea.TogSim.Model.Interfaces;

namespace Noea.TogSim.Model.Train.LocoDriver.States
{
    public class WaitSignalState : LocoState
    {
        LocoState _previous = null;
        public WaitSignalState()
            : base()
        {
            base._description = "Wait for signal";
        }
        public override LocoState Handle(LocoDriver_StPattern ld)
        {
            LocoState nextState;
            if (!ld.PreviousState.Equals(StateFactory.StoppingState) && !ld.PreviousState.Equals(this))
            {
                _previous = ld.PreviousState;
                Console.WriteLine("_previous: {0}", _previous.Description);
            }
            ITrack track = ld.Train.CurrentTrack;
            if (ld.HaveStopSignal())
            {
                //Console.WriteLine("Stop signal fundet");
                //ld.Train.ActualSpeed = 0;
                //ld.Train.Acceleration = 0;
                if (ld.Train.ActualSpeed > 0)
                {
                    nextState = StateFactory.StoppingState;
                }
                else
                {
                    nextState = this;
                }
            }
            else
            {
                nextState = _previous;
            }
            Console.WriteLine("WaitForSignal returnerer {0}", nextState.Description);
            return nextState;
        }
    }
}
